import { url } from '../config'
const { baseUrl } = url
export function request(url, method = 'GET', data = {}) {
  let fullUrl = `${baseUrl}${url}`
  let token = wx.getStorageSync('token') ? wx.getStorageSync('token') : ''
  return new Promise((resolve, reject) => {
    // wx.showLoading({
    //   title: '加载中'
    // })
    wx.request({
      url: fullUrl,
      method,
      data,
      header: {
        'content-type': 'application/json',
        'Authorization': token,
      },
      success(res) { 
        // wx.hideLoading()
        if (res.data.code === 0) {
          resolve(res.data.data)
        } else if (res.data.code === 4001 || res.data.code === 4011) {
          wx.clearStorageSync('islogin')
          wx.navigateTo({
            url: '/pages/login/login',
          })
          wx.showToast({
            title: '登陆失效请重新登陆',
            icon: 'none'
          })
          reject(res.data.message)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          reject(res.data.message)
        }
      },
      fail() {        
        // wx.hideLoading()
        wx.showToast({
          title: '当前网络不佳，请检查网络设置',
          icon: 'none'
        })
        reject('当前网络不佳，请检查网络设置')
      }
    })
  })
}